package com.qt.judge;

import com.qt.common.LanguageScriptEnum;
import com.qt.pojo.entity.judge.Judge;
import com.qt.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * @author pgw
 * @version 1.0
 * @description
 * @date Create by 2023/10/2 13:10
 */

@Component
@Slf4j(topic = "qtoj")
public class JudgeCompiler {

    private  String submissionPath;

    private  String codePath;

    private  String  buildingScript;

    private  String  judgeCorePath;


    private  String  compileInfoOutMAxSize;

    public static  List<String> compile(Judge judge){

        String[] envp = {"LANG=GBK"};
        Process process = null;
        String submissionPath = JudgeHolder.getSubmissionPath();

        LanguageScriptEnum language = LanguageScriptEnum.toLanguageType(judge.getLanguage());
        String judgeCoreScriptPath = JudgeHolder.getJudgeCoreScriptPath();
        String codePath = JudgeHolder.getCodePath(language.getExtensionName());
        String buildingScript = language.getBuildScriptByRunningPath(submissionPath, codePath);
        log.debug("languageScriptEnum:{}" , language);
       log.debug("submissionPath:{}" , submissionPath);
       log.debug("codePath:{}" , codePath);
       log.debug("judgeCoreScriptPath:{}" , judgeCoreScriptPath);
       log.debug("buildingScript:{}" ,buildingScript);

        try {

            process = Runtime.getRuntime().exec(
                    new String[]{
                            "/home/judge/compile.sh",
                            submissionPath,
                            codePath,
                            judge.getCode(),
                            buildingScript,
                            judgeCoreScriptPath,
                            "1",
                            "100000"
                    },envp,null);
            log.debug("编译完成,process:{}" , process);
            process.waitFor();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        List<String> compileResult =  readFile(submissionPath + "/compile.err");
        log.debug("编译结果为:{}" , compileResult);
        return compileResult;
    }



    /**
     * 读取某个文件的内容
     *
     * @param filePath 文件路径
     * @return List<String> 错误内容，我们用数组存储，用下标来代表行
     * @author yuzhanglong
     * @date 2020-7-1 9:22
     */
    private static List<String> readFile(String filePath) {
        List<String> judgeErrors = null;
        try {
            judgeErrors = FileUtil.readFileByLines(filePath);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        return judgeErrors;
    }

    public static void main(String[] args) {
        LanguageScriptEnum languageType = LanguageScriptEnum.toLanguageType("JAVA");
        String extensionName = languageType.getExtensionName();
        log.debug("name:{}" , extensionName);
    }
}
